define(function(require, exports, module) {
    require('basic');
    require('jquerytmpl');
    var config = require('config');
    var user = require('user');

    var model = function(){
        this.init();
        //console.log(config.login)
    };

    model.prototype ={
        init: function(){
            this.addEvents();
            this.loadList();
        },
        /**
         * [loadList /intranet/list 获取内网规则列表]
         * @return {[type]} [description]
         */
        loadList: function(){
            var sf = this;
            var data_str = user.info.leadPost;
            $.ajax({
                type: "GET",
                url: config.intranet_list+data_str,
                dataType: 'json',
                success: function(data){
                    for(var i=0;i<data.length;i++){
                        data[i]['index'] = i+1;
                        if(data[i]['alloced']){
                            data[i]['alloced_cn'] = "已分配";
                        }else{
                            data[i]['alloced_cn'] = "未分配";
                        }
                    }
                    $('#intranet_list tbody').html('');
                    $("#intranetListTemplate").tmpl(data).appendTo("#intranet_list tbody");

                    var wrap = $('#intranet_list'),
                        pop = $('#delete_intranet');
                    sf.checkAll(wrap);
                    setTimeout(function(){
                        sf.loadList();
                    },30000);

                    $('#delete_popbtn').off('click').on('click',function(){//删除内网规则
                        var checked_arr = [],
                            index = 0;
                        wrap.find('tbody input:checkbox').each(function(ind,el){
                            if($(this).prop('checked')){
                                var pot_ip_arr = $(this).data('pot_ip').split('/');
                                checked_arr.push({pot_ip:pot_ip_arr[0]});
                            }
                        });
                        if(checked_arr.length>0){
                            pop.find('tbody').html('');
                            $("#intranetDeleteTemplate").tmpl(checked_arr).appendTo(pop.find('tbody'));
                            pop.modal({keyboard: false, show: true});
                            $('#delete_btn').off('click').on('click',function(){
                                for(var o=0;o<checked_arr.length;o++){
                                    $.ajax({
                                        type: "DELETE",
                                        url: config.intranet+'/'+checked_arr[o]['pot_ip']+data_str,
                                        dataType: 'json',
                                        success: function(data){
                                            index++;
                                            if(index>=checked_arr.length){
                                                window.location.reload();
                                            };
                                        },
                                        error: function(e){
                                            $('#error_body').html('').html(e.responseText);
                                            $('#error').modal({keyboard: false, show: true});
                                        }
                                    });
                                };
                            });
                            $('#cancel_btn').off('click').on('click',function(){
                                pop.modal('hide');
                            });
                        }else{
                            alert('请选择资源池！');
                        };
                    });
                } 
            });
        },
        /**
         * [checkAll 全选动作]
         * @param  {[jqobj]} _wrap [description]
         * @return {[type]}       [description]
         */
        checkAll: function(_wrap){
            _wrap.find('input.checked_all').each(function(){
                $(this).removeAttr('checked');
                $(this).change(function(e) {
                    var bl = $(this).prop('checked');
                    _wrap.find('input.form_checkbox').prop({checked:bl});
                });
            });
            
        },
        /**
         * [addIntranet 添加内网规则]
         * @return {[type]} [description]
         */
        addIntranet: function(){
            var sf = this;
            var pop = $('#addIntranet');
            pop.modal({keyboard: false, show: true,}).off('shown.bs.modal').on('shown.bs.modal',function (e) {
                sf.addNewRule();
            });
        },

        /**
         * 添加一行新的规则
         * @param {[obj]} _obj [浮层上的加号按钮]
         */
        addNewRule: function(){
            var sf = this;
            var block = $('#addIntranetForm table');
            var wrap = $('#addIntranetForm tbody');
            var copy_data;
            var getValue = function(_tr){
                var tr_data = [];
                _tr.find('input').each(function(index,el){
                    tr_data[index] = $(el).val();
                });
                return tr_data;
            };
            var setValue = function(_tr,data){
                if(data){
                    _tr.find('input').each(function(index,el){
                        $(el).val(data[index]);
                    });
                }else{
                    _tr.find('input').each(function(index,el){
                        $(el).val('');
                    });
                };
            };
            function creatHtml(){
                var tr = $("#intTemplate").tmpl().appendTo(wrap);
                (function(_tr){
                    _tr.find('button').eq(0).off('click').on('click',function(e){
                       copy_data = getValue(_tr);
                    });
                })(tr);
                (function(_tr){
                    _tr.find('button').eq(1).off('click').on('click',function(e){
                        setValue(_tr,copy_data);
                    });
                })(tr);
                (function(_tr){
                    _tr.find('button').eq(2).off('click').on('click',function(e){
                        setValue(_tr);
                    });
                })(tr);
                (function(_tr){
                    _tr.find('button').eq(3).off('click').on('click',function(e){
                        _tr.remove();
                    });
                })(tr);
            };
            block.find('tfoot button').off().on('click',function(){
                creatHtml();
            });
            if(wrap.find('tr').length<=0){
                creatHtml();
            }
            
        },
        /**
         * 向接口提交添加的数据
         * @return {[type]} [description]
         */
        postIntanet: function(){
            var sf = this;
            var data_str = user.info.leadPost;
            var l = $('#addIntranetForm tbody tr').length;
            var sendArr = [];

            $('#addIntranetForm').find('tbody tr').each(function(){
                var obj = {};
                $(this).find('.form-control').each(function(index, el) {
                    if($(el).attr('name') == 'pot_prefix' || $(el).attr('name') == 'ca_prefix'){
                        obj[$(el).attr('name')] = Number($(el).val());
                    }else{
                        obj[$(el).attr('name')] = $(el).val();
                    }
                });
                sendArr.push(obj);
            });

            //console.log(sendArr);
            var postData = JSON.stringify(sendArr);
            $.ajax({
                type: "POST",
                url: config.intranet+data_str,
                data: postData,
                dataType: 'json',
                success: function(data){
                    if(data){
                        window.location.reload();
                    }
                },
                error: function(e){
                    $('#error_body').html('').html(e.responseText);
                    $('#error').modal({keyboard: false, show: true});
                }
            });
        },
        /**
         * [addEvents description]
         */
        addEvents: function(){
            var sf = this;
            $('#addIntr').off('click').on('click',function(){
                sf.addIntranet()
            }); //点击[添加]按钮，弹出添加内网规则浮层

            // $('#addIntranetForm').on('click','.addNew', function(){  //点击浮层上的加号按钮
            //     sf.addNewRule(this);
            // });
            $('#submit1').on('click',function(){  //点击提交浮层上的数据
                sf.postIntanet();
            });
        }
    };
    

    module.exports = new model();

    
});

